<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<title>JS-原型对象与字面对象-jqfocus.com</title>
		<link rel="stylesheet" type="text/css" href="css/global.css"></link>
		<link rel="stylesheet" type="text/css" href="css/style.css"></link>  
	</head>
	<body>
		<h2 class="demo-part">1，JS里面的原型对象与字面对象</h2>
		<div class="demo-part demo-ct">
			<ul>
				<li>原型对象的constructor属性引用(指向)创建该对象的类的构造函数</li>
				<li>字面对象的constructor属性引用(指向)Object类的构造函数</li>
				<li>字面对象本质上都是Object类的实体</li>
				<li>原型对象是对应原型类的实体</li>
				<li>JS世界里到处都是字面对象如JSON数据。关于字面对象，可参考<a target="_blank" href="http://www.dyn-web.com/tutorials/obj_lit.php">dyn-web.com/tutorials/obj_lit.php</a></li>
				<li>任何类(Class)都有一个原型(prototype)属性，该属性的构造器(constructor)属性指向声明该类的函数自身!</li>
			</ul>
			<div class="demo-tip">
				本文面向的JS读者:具备基本的面向对象编程知识
			</div>
		</div>
		<h2 class="demo-part">2，代码示例-可在firebug控制台中运行</h2>
		<div class="demo-part demo-ct">
			<pre class="brush:js;">
			/* 原型对象与字面对象(Prototype based Object and Literal Object) */
			
			/* 原型对象的constructor属性引用(指向)创建该对象的类的构造函数 */
			function Rabbit(adjective){
			  this.adjective=adjective;
			};
			Rabbit.prototype.speak=function(line){
			   alert("The "+ this.adjective+" rabbit says '"+line+"'");
			 };
			var rabbit1=new Rabbit("Red");
			rabbit1.speak("lady gaga");
			
			alert(rabbit1.constructor);
			alert(Rabbit.prototype.constructor);
			alert(rabbit1.constructor===Rabbit.prototype.constructor);
			
			/* 字面对象的constructor属性引用(指向)Object类的构造函数 */
			function makeRabbit(adjective){
			  return {
			    adjective:adjective,
			    speak:function(line){
			      alert("The "+ this.adjective+" rabbit says '"+line+"'");
			    }
			  };
			};
			
			var rabbit2=makeRabbit("Black");
			rabbit2.speak("miao miao");
			
			alert(rabbit2.constructor);
			alert(Object.prototype.constructor);
			alert(rabbit2.constructor===Object.prototype.constructor);
			
			/* 关于字面对象，可参考http://www.dyn-web.com/tutorials/obj_lit.php */
			
			
			/* 字面对象本质上都是Object类的实体*/
			/* 原型对象是对应原型类的实体 */
			
			/* toString方法都是继承自Object类 */
     		alert(rabbit1.toString===rabbit2.toString);			
			</pre>			
		</div>

	</body>
</html>
